<?php

class ModelFAItem extends Model {

    public function addItem($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "fa_item SET purchase_code = '" . $this->db->escape($data['purchase_code']) . "', sale_code = '" . $this->db->escape($data['sale_code']) . "', status = '" . (int) $data['status'] . "'");

        $item_id = $this->db->getLastId();

        //purchase price
        if (isset($data['purchase_price'])) {
            foreach ($data['purchase_price'] as $purchase_price) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "fa_purchase_price SET item_id = '" . $item_id . "', supplier_id = '" . (int) $purchase_price['supplier_id'] . "', price = '" . $purchase_price['price'] . "'");
            }
        }

        //sale price
        if (isset($data['item_sale_price'])) {
            foreach ($data['item_sale_price'] as $item_sale_price) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "fa_sale_price SET item_id = '" . $item_id . "', type_id = '" . (int) $item_sale_price['type_id'] . "', quantity = '" . $item_sale_price['quantity'] . "', price = '" . $item_sale_price['price'] . "'");
            }
        }

        if (isset($data['image'])) {
            $this->db->query("UPDATE " . DB_PREFIX . "fa_item SET image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "' WHERE item_id = '" . (int) $item_id . "'");
        }

        foreach ($data['item_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "fa_item_description SET item_id = '" . (int) $item_id . "', language_id = '" . (int) $language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "'");
        }

        $this->cache->delete('item');
    }

    public function editItem($item_id, $data) {
        $this->db->query("UPDATE " . DB_PREFIX . "fa_item SET purchase_code = '" . $this->db->escape($data['purchase_code']) . "', sale_code = '" . $this->db->escape($data['sale_code']) . "', group_id = '" . (int) $data['group_id'] . "', status = '" . (int) $data['status'] . "' WHERE item_id = '" . (int) $item_id . "'");

        if (isset($data['image'])) {
            $this->db->query("UPDATE " . DB_PREFIX . "fa_item SET image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "' WHERE item_id = '" . (int) $item_id . "'");
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_item_description WHERE item_id = '" . (int) $item_id . "'");

        foreach ($data['item_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "fa_item_description SET item_id = '" . (int) $item_id . "', language_id = '" . (int) $language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "'");
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_purchase_price WHERE item_id = '" . (int) $item_id . "'");
        //purchase price
        if (isset($data['purchase_price'])) {
            foreach ($data['purchase_price'] as $purchase_price) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "fa_purchase_price SET item_id = '" . (int) $item_id . "', supplier_id = '" . (int) $purchase_price['supplier_id'] . "', price = '" . $purchase_price['price'] . "'");
            }
        }

        //sale price
        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_sale_price WHERE item_id = '" . (int) $item_id . "'");
        if (isset($data['item_sale_price'])) {
            foreach ($data['item_sale_price'] as $item_sale_price) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "fa_sale_price SET item_id = '" . $item_id . "', type_id = '" . (int) $item_sale_price['type_id'] . "', quantity = '" . $item_sale_price['quantity'] . "', price = '" . $item_sale_price['price'] . "'");
            }
        }

        $this->cache->delete('item');
    }

    public function copyItem($item_id) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "fa_item p LEFT JOIN " . DB_PREFIX . "fa_item_description pd ON (p.item_id = pd.item_id) WHERE p.item_id = '" . (int) $item_id . "' AND pd.language_id = '" . (int) $this->config->get('config_language_id') . "'");

        if ($query->num_rows) {
            $data = array();

            $data = $query->row;
            $data['status'] = '0';

            $data = array_merge($data, array('item_description' => $this->getItemDescriptions($item_id)));

            $this->addItem($data);
        }
    }

    public function deleteItem($item_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_item WHERE item_id = '" . (int) $item_id . "'");

        //delete purchase price
        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_purchase_price WHERE item_id = '" . (int) $item_id . "'");

        //delete sale price
        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_sale_price WHERE item_id = '" . (int) $item_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_item_description WHERE item_id = '" . (int) $item_id . "'");

        $this->cache->delete('item');
    }

    public function getItem($item_id) {
        $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'item_id=" . (int) $item_id . "') AS keyword FROM " . DB_PREFIX . "fa_item p LEFT JOIN " . DB_PREFIX . "fa_item_description pd ON (p.item_id = pd.item_id) WHERE p.item_id = '" . (int) $item_id . "' AND pd.language_id = '" . (int) $this->config->get('config_language_id') . "'");

        return $query->row;
    }

    public function getItems($data = array()) {
        $sql = "SELECT * FROM " . DB_PREFIX . "fa_item p LEFT JOIN " . DB_PREFIX . "fa_item_description pd ON (p.item_id = pd.item_id)";
        
        if (!empty($data['filter_supplier'])) {
            $sql .= " LEFT JOIN " . DB_PREFIX . "fa_purchase_price pr ON (p.item_id = pr.item_id)";
        }
        
        $sql .= " WHERE pd.language_id = '" . (int) $this->config->get('config_language_id') . "'";
        
        if (isset($data['filter_supplier'])) {
            $sql .= " AND pr.supplier_id = '" . (int) $data['filter_supplier'] ."'"; 
        }
        
        if (!empty($data['filter_group']) && $data['filter_group'] != '*' && $data['filter_group'] != '0') {
            $sql .= " AND p.group_id = '" . (int) $data['filter_group'] . "'";
        }
        
        if (!empty($data['filter_name'])) {
            $sql .= " AND pd.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
        }

        if (!empty($data['filter_purchase_code'])) {
            $sql .= " AND p.purchase_code LIKE '" . $this->db->escape($data['filter_purchase_code']) . "%'";
        }

        if (!empty($data['filter_sale_code'])) {
            $sql .= " AND p.sale_code LIKE '" . $this->db->escape($data['filter_sale_code']) . "%'";
        }

        if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
            $sql .= " AND p.status = '" . (int) $data['filter_status'] . "'";
        }

        $sql .= " GROUP BY p.item_id";

        $sort_data = array(
            'pd.name',
            'p.group_id',
            'p.purchase_code',
            'p.sale_code',
            'p.quantity',
            'p.status',
            'p.sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY pd.name";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
        }
        
        $query = $this->db->query($sql);
        
        return $query->rows;
    }

    public function getItemDescriptions($item_id) {
        $item_description_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_item_description WHERE item_id = '" . (int) $item_id . "'");

        foreach ($query->rows as $result) {
            $item_description_data[$result['language_id']] = array(
                'name' => $result['name'],
                'description' => $result['description'],
            );
        }

        return $item_description_data;
    }

    public function getTotalItems($data = array()) {
        $sql = "SELECT COUNT(DISTINCT p.item_id) AS total FROM " . DB_PREFIX . "fa_item p LEFT JOIN " . DB_PREFIX . "fa_item_description pd ON (p.item_id = pd.item_id)";
        
        if (!empty($data['filter_supplier'])) {
            $sql .= " LEFT JOIN " . DB_PREFIX . "fa_purchase_price pr ON (p.item_id = pr.item_id)";
        }
        
        $sql .= " WHERE pd.language_id = '" . (int) $this->config->get('config_language_id') . "'";
        
        if (!empty($data['filter_supplier'])) {
            $sql .= " AND pr.supplier_id = " . (int) $data['filter_supplier']; 
        }
        
        if (!empty($data['filter_group']) && $data['filter_group'] != '*' && $data['filter_group'] != '0') {
            $sql .= " AND p.group_id = '" . (int) $data['filter_group'] . "'";
        }
        
        if (!empty($data['filter_name'])) {
            $sql .= " AND pd.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
        }

        if (!empty($data['filter_purchase_code'])) {
            $sql .= " AND p.model LIKE '" . $this->db->escape($data['filter_purchase_code']) . "%'";
        }

        if (!empty($data['filter_sell_code'])) {
            $sql .= " AND p.price LIKE '" . $this->db->escape($data['filter_sell_code']) . "%'";
        }

        if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
            $sql .= " AND p.status = '" . (int) $data['filter_status'] . "'";
        }

        $query = $this->db->query($sql);

        return $query->row['total'];
    }

    public function getItemPurchasePrice($item_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_purchase_price WHERE item_id = '" . (int) $item_id . "'");
        return $query->rows;
    }

    public function getItemSalePrice($item_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_sale_price WHERE item_id = '" . (int) $item_id . "'");
        return $query->rows;
    }  
}

?>
